import isEmpty from 'loadsh/isEmpty'
import { GetTapeoutTopic, GetTapeoutTopicById, GetTopic, GetTopicById, PostTapeoutTopic, PostTopic, TapeoutTopicDeleteData, TopicDeleteData } from '/constData/requestFun/_teseRequest';
import compact from 'loadsh/compact'
import { SPACEID } from '/constData/requestFun/_globalData';
Page({
  data: {
    item: 1,
    min: null,
    max: new Date('2033/12/02'),
    defaultDate: new Date(),
    TapeOutIssuesInfo: {
      CorpId: '',//申请人
      ApplicatDepart: '',//申请部门
      TopicTitle: '', //议题标题
      TopicContent: '',//议题内容
      ReviewConference: '',//审议会议
      MeetingTime: '',//会议时间
      Participants: '武总 谢总 梁律 刘部', //参会人员
      Annex: '[]', //附件
      Notes: '',//备注
      LawyerApproval: '',//是否需要律师审批
    },
    ReceiveData: [],
    displaydata: [],
  },
  Delete(e) {
    const id = e.currentTarget.dataset.id
    TapeoutTopicDeleteData(id, (res) => {
      my.alert({
        title: '撤销成功',
      });
      //刷新数据
      this.setData({
        ReceiveData: this.data.ReceiveData.filter((it) => {
          return it.item1.id !== id
        })
      })
    }, (err) => {
      my.alert({
        title: '撤销失败',
      });
    })
  },
  onLawyerApprovalChange(date) {
    this.setData({
      TapeOutIssuesInfo: {
        ...this.data.TapeOutIssuesInfo,
        LawyerApproval: date
      }
    })
  },
  onNotesChange(date) {
    this.setData({
      TapeOutIssuesInfo: {
        ...this.data.TapeOutIssuesInfo,
        Notes: date
      }
    })
  },
  onTopicTitleChange(date) {
    this.setData({
      TapeOutIssuesInfo: {
        ...this.data.TapeOutIssuesInfo,
        TopicTitle: date
      }
    })
  },
  handleOk(date, format, e) {
    this.setData({
      TapeOutIssuesInfo: {
        ...this.data.TapeOutIssuesInfo,
        MeetingTime: date
      },
    })
  },
  onApplicatDepartChange(e) {
    this.setData({
      TapeOutIssuesInfo: {
        ...this.data.TapeOutIssuesInfo,
        ApplicatDepart: e
      }
    })
  },
  onTopicContentChange(date) {
    this.setData({
      TapeOutIssuesInfo: {
        ...this.data.TapeOutIssuesInfo,
        TopicContent: date
      }
    })
  },
  onReviewConferenceChange(date) {
    this.setData({
      TapeOutIssuesInfo: {
        ...this.data.TapeOutIssuesInfo,
        ReviewConference: date
      }
    })
  },
  // 选择同行人
  colleague() {
    my.complexChoose({
      title: "测试标题", //标题
      multiple: true, //是否多选
      limitTips: "超出了", //超过限定人数返回提示
      maxUsers: 1000, //最大可选人数
      pickedUsers: [], //已选用户，值为userId列表
      pickedDepartments: [], //已选的部门id
      disabledUsers: [], //不可选用户，值为userId列表
      disabledDepartments: [], //不可选部门id
      requiredUsers: [], //必选用户（不可取消选中状态）
      requiredDepartments: [], //必选部门（不可取消选中状态）
      permissionType: "xxx", //可添加权限校验，选人权限，目前只有GLOBAL这个参数
      responseUserOnly: false, //返回人，或者返回人和部门
      success: (res) => {
        // this.setData({
        //   topicsinfo: {
        //     ...this.data.topicsinfo,
        //     Participants: [],
        //   }
        // })
        // for (let index = 0; index < res.users.length; index++) {
        //   this.setData({
        //     topicsinfo: {
        //       ...this.data.topicsinfo,
        //       Participants: [...this.data.topicsinfo.Participants, res.users[index].name],
        //     }
        //   })
        // }
      },
      fail: function (err) { }
    })
  },

  //获取数据
  onLoad() {
    my.getStorage({
      key: 'userInfo',
      success: (storage) => {
        GetTapeoutTopic((res) => {
          const cacheData = res.data.map(it => {
            if (it.item1.corpId === storage.data.result.userid) {
              it.item1['displayAnnex'] = JSON.parse(it.item1.annex)
              return it
            }
          })
          this.setData({
            TapeOutIssuesInfo: {
              ...this.data.TapeOutIssuesInfo,
              CorpId: storage.data.result.userid
            },
            ReceiveData: compact(cacheData).reverse()
          })
        })
      }
    })
  },
  // 上传数据
  onSubmit(e) {
    PostTapeoutTopic(JSON.stringify(this.data.TapeOutIssuesInfo),
      (res) => {
        my.alert({
          title: '提交成功',
        });
        GetTapeoutTopicById(res.data, (res) => {
          this.setData({
            ReceiveData: compact([...this.data.ReceiveData, res.data].map(it => {
              if (it.item1.corpId === this.data.TapeOutIssuesInfo.CorpId) {
                it.item1['displayAnnex'] = JSON.parse(it.item1.annex)
                return it
              }
            })).reverse()
          })
        })
        this.setData({
          TapeOutIssuesInfo: {
            ...this.data.TapeOutIssuesInfo,
            ApplicatDepart: '',//申请部门
            TopicTitle: '', //议题标题
            TopicContent: '',//议题内容
            ReviewConference: '',//审议会议
            MeetingTime: '',//会议时间
            Participants: '武总 谢总 梁律 刘部', //参会人员
            Annex: '[]', //附件
            Notes: '',//备注
            LawyerApproval: '',//是否需要律师审批
          }
        })
      },
      (err) => {
        my.alert({
          title: '提交成功1',
        });
      })
  },

  //上传图片
  onUpload() {
    let imageAnnex = ''
    dd.uploadAttachmentToDingTalk({
      image: {
        max: 9,
        spaceId: SPACEID,
        compress: true,
        folderId: '',
        multiple: true,
      },
      types: ['photo'],
      success: (res) => {
        const srcAnnex = this.data.TapeOutIssuesInfo.Annex
        imageAnnex = res.data
        imageAnnex.push(...JSON.parse(srcAnnex))
        this.setData({
          displaydata: imageAnnex,
          TapeOutIssuesInfo: {
            ...this.data.TapeOutIssuesInfo,
            Annex: JSON.stringify(imageAnnex)
          }
        })
      },
      fail: () => { },
      complete: () => { },
    });
  },
  PreviewBtn(e) {
    const item = e.currentTarget.dataset.item
    dd.previewFileInDingTalk({
      spaceId: item.spaceId,
      fileName: item.fileName,
      corpId: 'ding1b9790fbb37a3cc7f5bf40eda33b7ba0',
      fileSize: item.fileSize,
      fileType: item.fileType,
      fileId: item.fileId,
    });
  },
  CheckPictures(e) {
    const item = e.currentTarget.dataset.item
    dd.previewFileInDingTalk({
      spaceId: item.spaceId,
      fileName: item.fileName,
      corpId: 'ding1b9790fbb37a3cc7f5bf40eda33b7ba0',
      fileSize: item.fileSize,
      fileType: item.fileType,
      fileId: item.fileId,
    });
  },

});